package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BizReceiptNote;
import com.ruoyi.system.service.IBizReceiptNoteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收料通知单Controller
 * 
 * @author ruoyi
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/system/receipt")
public class BizReceiptNoteController extends BaseController
{
    @Autowired
    private IBizReceiptNoteService bizReceiptNoteService;

    @Autowired
    private com.ruoyi.web.support.SupplierContext supplierContext;

    /**
     * 查询收料通知单列表
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizReceiptNote bizReceiptNote)
    {
        supplierContext.resolveCurrentSupplierCode().ifPresent(bizReceiptNote::setSupplierCode);
        startPage();
        List<BizReceiptNote> list = bizReceiptNoteService.selectBizReceiptNoteList(bizReceiptNote);
        return getDataTable(list);
    }

    /**
     * 导出收料通知单列表
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:export')")
    @Log(title = "收料通知单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizReceiptNote bizReceiptNote)
    {
        supplierContext.resolveCurrentSupplierCode().ifPresent(bizReceiptNote::setSupplierCode);
        List<BizReceiptNote> list = bizReceiptNoteService.selectBizReceiptNoteList(bizReceiptNote);
        ExcelUtil<BizReceiptNote> util = new ExcelUtil<BizReceiptNote>(BizReceiptNote.class);
        util.exportExcel(response, list, "收料通知单数据");
    }

    /**
     * 获取收料通知单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:query')")
    @GetMapping(value = "/{receiptNoteId}")
    public AjaxResult getInfo(@PathVariable("receiptNoteId") Long receiptNoteId)
    {
        return success(bizReceiptNoteService.selectBizReceiptNoteByReceiptNoteId(receiptNoteId));
    }

    /**
     * 新增收料通知单
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:add')")
    @Log(title = "收料通知单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizReceiptNote bizReceiptNote)
    {
        int rows = bizReceiptNoteService.insertBizReceiptNote(bizReceiptNote);
        if (rows > 0) {
            // 插入成功后，bizReceiptNote 已包含生成的 receiptNoteId 与 receiptNoteNo
            return AjaxResult.success(bizReceiptNote);
        }
        return AjaxResult.error("新增收料通知单失败");
    }

    /**
     * 修改收料通知单
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:edit')")
    @Log(title = "收料通知单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizReceiptNote bizReceiptNote)
    {
        return toAjax(bizReceiptNoteService.updateBizReceiptNote(bizReceiptNote));
    }

    /**
     * 删除收料通知单
     */
    @PreAuthorize("@ss.hasPermi('system:receipt:remove')")
    @Log(title = "收料通知单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiptNoteIds}")
    public AjaxResult remove(@PathVariable Long[] receiptNoteIds)
    {
        return toAjax(bizReceiptNoteService.deleteBizReceiptNoteByReceiptNoteIds(receiptNoteIds));
    }
}
